package jai.zoom;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.border.BevelBorder;

import com.sun.media.jai.widget.DisplayJAI;

/**
 * Original Author's note: (https://jaistuff.dev.java.net/display.html) This
 * class demonstrates the usage of the class DisplayThumbnail. It will create an
 * instance of DisplayThumbnail and an instance of DisplayJAI controlled by it.
 * Never mind the layout stuff, I was just playing with SpringLayout.
 */

/*
 * Modify some code to enable go to the next image if you pass array of images
 * as arguments
 * 
 */

public class DisplayThumbnailApp extends JFrame implements ActionListener,
		MouseMotionListener, MouseListener {
	// The instance of DisplayThumbnail.
	private DisplayThumbnail dt;

	// The instance of DisplayJAI.
	private DisplayJAI dj;

	// Two labels to show the world (original image) and thumbnail viewport
	// coordinates.
	private JLabel world, view;

	// to keep all the signatures
	private PlanarImage[] sources;

	// just testing move it outside
	private JPanel borderDisplay;

	private Container contentPane;

	private SpringLayout layout;

	private int currentSourceIndex;

	/**
	 * The constructor for the class, which will take as arguments the original
	 * image and the desired width and height of the region to be shown.
	 * 
	 * @param image
	 *            the original image to be displayed
	 * @param dWidth
	 *            the desired width for the region to be shown.
	 * @param dHeight
	 *            the desired height for the region to be shown.
	 */
	public DisplayThumbnailApp(String[] args, int dWidth, int dHeight) {
		super("Interactive Thumbnail Example");

		currentSourceIndex = 0;
		setSources(args);

		layout = new SpringLayout();
		contentPane = getContentPane();
		contentPane.setLayout(layout);

		// First we create the instance of DisplayThumbnail with a 0.1 scale.
		dt = new DisplayThumbnail(sources[0], 0.1f, dWidth, dHeight);
		// We must register mouse motion listeners to it !
		dt.addMouseMotionListener(this);
		dt.addMouseListener(this);

		// Now we create the instance of DisplayJAI to show the region
		// corresponding to the viewport.
		dj = new DisplayJAI(dt.getImage());
		// Set it size.
		dj.setPreferredSize(new Dimension(dWidth, dHeight));
		dj.setMinimumSize(new Dimension(dWidth, dHeight));
		dj.setMaximumSize(new Dimension(dWidth, dHeight));
		// Create a JPanel so we can add a border around the instance of
		// DisplayJAI.
		borderDisplay = new JPanel(new BorderLayout());
		borderDisplay.add(dj);
		borderDisplay.setBorder(BorderFactory
				.createBevelBorder(BevelBorder.RAISED));
		// Create a small panel with the labels.
		JLabel worldL = new JLabel("World: ");
		world = new JLabel("");
		JLabel viewL = new JLabel("Thumb: ");
		view = new JLabel("");
		// Let's work with the Spring Layout then.
		contentPane.add(dt);
		contentPane.add(borderDisplay);
		contentPane.add(worldL);
		contentPane.add(world);
		contentPane.add(viewL);
		contentPane.add(view);

		// East of DisplayThumbnail is glued to East of the container.
		layout.putConstraint(SpringLayout.EAST, dt, 5, SpringLayout.EAST,
				contentPane);
		// North of DisplayThumbnail is glued to North of the container.
		layout.putConstraint(SpringLayout.NORTH, dt, 5, SpringLayout.NORTH,
				contentPane);

		// East of DisplayJAI is glued to West of DisplayThumbnail.
		layout.putConstraint(SpringLayout.EAST, borderDisplay, -10,	SpringLayout.WEST, dt);
			

		// WEST of DisplayJAI is glued to WEST of the container.
		layout.putConstraint(SpringLayout.WEST, borderDisplay, 5,
				SpringLayout.WEST, contentPane);

		// North of DisplayJAI is glued to North of the container.
		layout.putConstraint(SpringLayout.NORTH, borderDisplay, 5,
				SpringLayout.NORTH, contentPane);

		// South of DisplayJAI is glued to South of the container.
		layout.putConstraint(SpringLayout.SOUTH, contentPane, 5,
				SpringLayout.SOUTH, borderDisplay);

		// West of DisplayJAI is glued to West of the container.
		layout.putConstraint(SpringLayout.WEST, contentPane, 5,
				SpringLayout.WEST, borderDisplay);

		// West of worldL is glued to East of the container.
		layout.putConstraint(SpringLayout.WEST, worldL, 5, SpringLayout.EAST,
				borderDisplay);
		// North of worldL is glued to South of DisplayThumbnail.
		layout.putConstraint(SpringLayout.NORTH, worldL, 5, SpringLayout.SOUTH,
				dt);
		// WEst of viewL is glued to East of the container.
		layout.putConstraint(SpringLayout.WEST, viewL, 5, SpringLayout.EAST,
				borderDisplay);
		// North of viewL is glued to South of worldL.
		layout.putConstraint(SpringLayout.NORTH, viewL, 5, SpringLayout.SOUTH,
				worldL);
		// East of world is glued to West of worldL.
		layout.putConstraint(SpringLayout.WEST, world, 0, SpringLayout.EAST,
				worldL);

		// North of world is glued to South of DisplayThumbnail.
		layout.putConstraint(SpringLayout.NORTH, world, 5, SpringLayout.SOUTH,
				dt);
		// West of view is glued to East of viewL.
		layout.putConstraint(SpringLayout.WEST, view, 0, SpringLayout.EAST,
				viewL);
		// North of view is glued to South of world.
		layout.putConstraint(SpringLayout.NORTH, view, 5, SpringLayout.SOUTH,
				world);

		final JButton nextButton = new JButton();
		nextButton.addActionListener(this);

		nextButton.setText("Next");
		getContentPane().add(nextButton);
		layout.putConstraint(SpringLayout.EAST, nextButton, -87,
				SpringLayout.EAST, getContentPane());
		layout.putConstraint(SpringLayout.NORTH, nextButton, 226,
				SpringLayout.NORTH, view);
		layout.putConstraint(SpringLayout.WEST, nextButton, -182,
				SpringLayout.EAST, getContentPane());

		// Set the closing operation so the application is finished.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// Don't let the user resize the window.
		// setResizable(false);

		pack(); // set an adequated size for the frame
		setExtendedState(MAXIMIZED_BOTH);
		setVisible(true); // show the frame.
		

	}

	/*
	 * 
	 * 
	 */
	public void mousePressed(MouseEvent e) {
		debug("===App.mousePressed====");
		// Change the image on the DisplayJAI component to correspond to the
		// viewport on the thumbnail.
		dj.set(dt.getImage());
		// Gets some information about the viewport and cropped image.
		Rectangle crop = dt.getCroppedImageBounds();
		Rectangle viewp = dt.getViewportBounds();
		// Change the labels' contents with this information.
		world.setText("" + crop.x + "," + crop.y + " (" + crop.width + "x"
				+ crop.height + ")");
		view.setText("" + viewp.x + "," + viewp.y + " (" + viewp.width + "x"
				+ viewp.height + ")");

	}

	public void mouseClicked(MouseEvent e) {

	}

	/**
	 * This method will be called when we drag the mouse over the thumbnail.
	 * 
	 */
	public void mouseDragged(MouseEvent e) {

		debug("App.mouseDragged ");

		// Change the image on the DisplayJAI component to correspond to the
		// viewport on the thumbnail.
		dj.set(dt.getImage());
		// Gets some information about the viewport and cropped image.
		Rectangle crop = dt.getCroppedImageBounds();
		Rectangle viewp = dt.getViewportBounds();
		// Change the labels' contents with this information.
		world.setText("" + crop.x + "," + crop.y + " (" + crop.width + "x"
				+ crop.height + ")");
		view.setText("" + viewp.x + "," + viewp.y + " (" + viewp.width + "x"
				+ viewp.height + ")");
	}

	/**
	 * This method is here just to keep the MouseMotionListener interface happy.
	 */
	public void mouseMoved(MouseEvent e) {
	}

	public static void debug(String arg) {
		System.out.println(arg);

	}

	private void setSources(String[] args) {
		sources = new PlanarImage[args.length];
		for (int i = 0; i < args.length; i++) {
			sources[i] = JAIImageReader.readImage(args[i]);
		}

	}

	// 
	public void repaint() {

	}

	public void actionPerformed(ActionEvent e) {
		debug("call to actionPerformed : " + e.getActionCommand());

		// handle the action, if you need to add previous you'll have to handle
		// it here
		if (currentSourceIndex < sources.length)
			currentSourceIndex++;

		dt.set(sources[currentSourceIndex], 640, 640);
		// below code dj.set() works well
		dj.set(dt.getImage());

		repaint();

	}

	/**
	 * The application entry point, which will load the image passed as the
	 * command-line argument and create an instance of the application.
	 */
	public static void main(String[] args) {
		// PlanarImage image = JAI.create("fileload", args[0]);
		
		DisplayThumbnailApp app = new DisplayThumbnailApp(args, 640, 640);

	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

}
